<?php

namespace app\api\middleware;

use think\facade\Db;
use util\JwtUtils;

class CheckOwner
{
    public function handle($request, \Closure $next)
    {

        $accessToken=request()->header("accessToken");
        if(empty($accessToken)){
            return json(["code"=>403,'msg' => "用户未登录"]);
        }
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $request->accessToken=  $accessToken;
        $request->openid=  $userResult["userid"];
        $room_user=Db::table("room_user")
            ->where("user_id",$userResult["userid"])
            ->where("type",1)
            ->find();
        if(empty($room_user)){
            return json(["code"=>500,'msg' => "该用户不是业主无权操作"]);
        }
        $request->room_user=$room_user;
        return $next($request);  //返回request闭包 固定格式写到这里
    }
}